var app = getApp();
Page({
  onLoad(open) {
    this.setData({
      id:open.id
    })
    console.log(this.data.id)
  },
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    sk: true,
    what_type:'phone',
    choose_color: '',
    choose_version:'',
    choose_com:'',
    choose_num:1,
    choose_money:'0',
    id:'',
    then:'',
    user:'',
    buys:[
      {
        'title':'Apple iPhone X',
        'img_0':'../img/20180907081727.png',
        'img_1': '../img/20180907081754.png',
        'money':'7099'
      },
      {
        'color':'颜色',
        'choss_version':'版本',
        'com':'内存',
        'num':'数量'
      }
    ],
    buys_choose:[
      {
        'color':['银色','深空灰'],
        'chosse_version':['公开版','双网通'],
        'com':['64G','256G']
      },
    ]
  },
  user_color:function(e){
    this.setData({
      choose_color:e.currentTarget.id
    })
  },
  user_version:function(e){
    this.setData({
      choose_version: e.currentTarget.id
    })
  },
  user_com:function(e){
    this.setData({
      choose_com: e.currentTarget.id
    })
  },
  up:function(e){
    console.log(e)
    this.setData({
      choose_num: this.data.choose_num+1
    })
  },
  down:function(e){
    console.log(e)
    if (this.data.choose_num>1) {
    this.setData({
      choose_num: this.data.choose_num - 1
    })
    }
  },
  on_this:function(){
    var then = {};
    var color = this.data.choose_color;
    var version = this.data.choose_version;
    var com = this.data.choose_com;
    var num = this.data.choose_num;
    var img = this.data.buys[0].img_1;
    var money = this.data.choose_money;
    var id=this.data.id
    var contrast = {
      'one_color': this.data.buys_choose[0].color[0],
      'two_color': this.data.buys_choose[0].color[1],
      'one_version': this.data.buys_choose[0].chosse_version[0],
      'two_version': this.data.buys_choose[0].chosse_version[1],
      'three_version': this.data.buys_choose[0].chosse_version[2],
      'four_version': this.data.buys_choose[0].chosse_version[3],
      'one_com': this.data.buys_choose[0].com[0],
      'two_com': this.data.buys_choose[0].com[1],
    }
    for (var i in contrast) {
      if (color == i) {
        then.color = contrast[color]
      } if (version == i) {
        then.version = contrast[version]
      } if (com == i) {
        then.com = contrast[com]
      }
    };
    then.num = num
    then.img = img
    then.money=money
    then.id=id;
    then.title = this.data.buys[0].title
    this.setData({
      then: then
    })
    if (then.color == '银色'  &&then.version=='公开版'&&then.com=='64G'){
      this.setData({
        choose_money:'7099'
      })
    } else if (then.color == '银色'  && then.version == '双网通' && then.com == '64G'){
      this.setData({
        choose_money: '6899'
      })
    } else if (then.color == '银色' && then.version == '公开版' && then.com == '256G'){
      this.setData({
        choose_money: '8099'
      })
    } else if (then.color == '银色'  && then.version == '双网通' && then.com == '256G'){
      this.setData({
        choose_money: '7899'
      })
    } else if (then.color == '深空灰' && then.version == '公开版' && then.com == '64G') {
      this.setData({
        choose_money: '7099'
      })
    } else if (then.color == '深空灰' && then.version == '双网通' && then.com == '64G') {
      this.setData({
        choose_money: '6899'
      })
    } else if (then.color == '深空灰' && then.version == '公开版' && then.com == '256G') {
      this.setData({
        choose_money: '8099'
      })
    } else if ( then.color == '深空灰' && then.version == '双网通' && then.com == '256G') {
      this.setData({
        choose_money: '7899'
      })
    }
    console.log(this.data.then)
    return then;
  },
  buys:function(e){
    //console.log(img)
    var arr = Object.keys(app.user)
    if (arr.length == 0) {
      wx.showToast({
        title: '请在我的界面进行登录',
        icon: 'none',
      })
    }else{
      var color = this.data.choose_color;
      var version = this.data.choose_version;
      var com = this.data.choose_com;
      var on = this.on_this()
      console.log(on)
      if (color != '' && version != '' && com != '') {
        var model = JSON.stringify(on);
        wx.navigateTo({
          url: '../buys_determine/buys_determine?model=' + model,
        });
      } else {
        wx.showToast({
          title: '请选择正确',
          icon: 'none'
        })
      }
    }
     
  },
  buys_car:function(e){
    var arr = Object.keys(app.user)
    console.log(arr.length)
    if(arr.length==0){
        wx.showToast({
          title: '请在我的界面进行登录',
          icon: 'none',
        })
    }else{
      var color = this.data.choose_color;
      var version = this.data.choose_version;
      var com = this.data.choose_com;
      var on = this.on_this();
      console.log(on)
      app.title = '123'
      if (color != '' && version != '' && com != '') {
        app.buys_car.id = on.id;
        app.buys_car.title = on.title;
        app.buys_car.color = on.color;
        app.buys_car.version = on.version;
        app.buys_car.com = on.com;
        app.buys_car.money = on.money;
        app.buys_car.img = on.img;
        app.buys_car.choose_num = on.num;
        app.buys_car.total_number = on.money;
        wx.showToast({
          title: '添加购物车成功',
          icon: 'success'
        })
      } else {
        wx.showToast({
          title: '请选择正确',
          icon: 'none'
        })
      } 
    }
    
  },
  buy_car_url:function(){
    wx.switchTab({
      url: '../pages/shopping/shopping'
    })
  }  
})